package com.projectsexception.festivales;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

import com.projectsexception.datandroid.Datandroid;
import com.projectsexception.datandroid.DatandroidFactory;
import com.projectsexception.festivales.setup.FeedInfo;
import com.projectsexception.festivales.setup.MapInfo;
import com.projectsexception.festivales.setup.ProjectResources;
import com.projectsexception.festivales.setup.ShowInfo;
import com.projectsexception.festivales.setup.xml.SetupParser;

public class SetupActivity extends Activity {
    
    private static final String INITIALIZED = "initialized";
    private ProgressBar progressBar;
    private Button continueButton;
    
    private class SetupTask extends AsyncTask<Void, Integer, Boolean> {
        
            private SetupParser parser;
            
            @Override
            protected void onPreExecute() {
                parser = new SetupParser();
            }
    
            @Override
            protected Boolean doInBackground(Void... voids) {
                Datandroid data = DatandroidFactory.getDatandroid();
                
                try {
                    data.open(SetupActivity.this, R.xml.database);
                    
                    List<FeedInfo> feeds = parser.parseFeeds(getResources().getXml(R.xml.news));
                    publishProgress(20);
                    
                    data.insertAll(feeds);
                    publishProgress(40);
                    
                    List<MapInfo> maps = parser.parseMaps(getResources().getXml(R.xml.maps));                
                    data.insertAll(maps);
                    publishProgress(60);
                    
                    List<ShowInfo> shows = parser.parseShows(getResources().getXml(R.xml.shows));
                    publishProgress(80);
                    
                    data.insertAll(shows);
                    publishProgress(100);
                    
                    data.close();
                } catch (Exception e) {
                    Log.e("", e.getLocalizedMessage());
                    return false;
                }
                
                return true;
            }
            
            @Override
            protected void onProgressUpdate(Integer... values) {
                progressBar.setProgress(values[0]);
            }
            
            @Override
            protected void onPostExecute(Boolean result) {
                endTask();
            }
        }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ProjectResources.initialize(this);
        setContentView(R.layout.setup);
        
        progressBar = (ProgressBar) findViewById(R.id.progress);
        continueButton = (Button) findViewById(R.id.continueButton);
        continueButton.setOnClickListener(new OnClickListener() {            
            @Override
            public void onClick(View arg0) {
                startActivity();
            }
        });
        Button button = (Button) findViewById(R.id.aboutButton);
        button.setOnClickListener(new OnClickListener() {            
            @Override
            public void onClick(View arg0) {
                showAbout();
            }
        });
        button = (Button) findViewById(R.id.exitButton);
        button.setOnClickListener(new OnClickListener() {            
            @Override
            public void onClick(View arg0) {
                finish();
            }
        });
        
        SharedPreferences settings = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE);
        if (settings.contains(INITIALIZED)) {
            endTask();
        } else {
            new SetupTask().execute(new Void[0]);
            Editor editor = settings.edit();
            editor.putBoolean(INITIALIZED, true);
            editor.commit();
        }
    }
    
    private void endTask() {
        progressBar.setVisibility(View.INVISIBLE);
        continueButton.setVisibility(View.VISIBLE);
    }
    
    private void startActivity() {
        startActivity(new Intent(this, MainActivity.class));
    }
    
    private void showAbout() {
        Context context = getApplicationContext();
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(getString(R.string.app_name));
        builder.setIcon(R.drawable.icon);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
        View about = inflater.inflate(R.layout.about, null);
        builder.setView(about);
        builder.setPositiveButton("OK", null);
        builder.create();
        builder.show();
    }

}
